package org.imlinker.message;

import javax.swing.JOptionPane;

import org.jivesoftware.smack.packet.Presence;

import org.imlinker.LocalVariablesManager;
import org.imlinker.list.ViewList;
import org.imlinker.login.Account;

/**
 * The request message of the other people want to add you.It's used as a
 * message in message list.
 * 
 * AwakenRz:I think this should be merged to another class.
 * 
 * 
 */

public class SubscribeRequest extends MessageItem {

	/**
	 * The user want to subscribe.
	 */
	private String user;

	/**
	 * 
	 * @param paraUser
	 *            The user of buddy which want to subscribe.
	 */
	public SubscribeRequest(final String paraUser) {
		super(null, paraUser);
		this.user = paraUser;
	}

	/**
	 * Get String infor.
	 * 
	 * @return The String infor.
	 */
	public final String toString() {
		return "A subcribeRequest";
	}

	/**
	 * DoubleClick event.
	 * 
	 */
	public final void doubleClick() {
		int i = JOptionPane.showConfirmDialog(null, user
				+ " "
				+ LocalVariablesManager
						.getLocalVariables("SubscribeRequestConfirm.Info"),
				LocalVariablesManager
						.getLocalVariables("SubscribeRequestConfirm.Title"),
				JOptionPane.YES_NO_OPTION);

		if (i == JOptionPane.OK_OPTION) {
			Presence presence = new Presence(Presence.Type.subscribed);
			presence.setTo(user);
			// Here need Username with JID
			presence.setFrom(Account.getUserFullID());
			Account.sendPacket(presence);
		} else {
			Presence presence = new Presence(Presence.Type.unsubscribed);
			presence.setTo(user);
			presence.setFrom(Account.getUserFullID());
			Account.sendPacket(presence);
		}

		ViewList.removeMessage(this);
	}

}
